package com.guge.sports.utils;


import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

//统一返回结果的类
@Data
public class R {

    public static Integer SUCCESS = 200; //成功

    public static Integer ERROR = 500; //失败

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据Map")
    private Map<String, Object> data = new HashMap<String, Object>();

    /*@ApiModelProperty(value = "返回数据Object")
    private Object  ;*/


    //把构造方法私有
    private R() {
    }

    //成功静态方法
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(SUCCESS);
        r.setMessage("成功");
        return r;
    }

    //失败静态方法
    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ERROR);
        r.setMessage("失败");
        return r;
    }

    //设置是否成功
    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    //设置返回信息
    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    //设置响应码
    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    //设置响应数据
    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

    public static R result(boolean b) {
        return b ? ok() : error();
    }
}
